#!/bin/sh

#-------------------------
# 功能：打包 .a 文件，用于制作sdk
#
# 使用方法：
# 1、把脚本拖入终端.
# 2、把项目拖进也拖进终端. 回车就 OK.
#
# 注意事项：如果没有成功报出问题:
# Permission denied。就是没有权限.
#
# 解决办法：
# 1、为其添加可执行权限，使用命令：chmod +x packageScript.sh (packageScript.sh 为脚本绝对路径).
# 2、修改该文件 脚本.sh 的权限，使用命令： 
# sudo chmod 777 脚本的绝对目录.
# 然后再执行就 OK.
#-------------------------


version=$1
# 检验参数
if [ -z $version ];then
echo "版本号没有填写"
exit 1
fi

buildDir="./PackageDir"
libName="libZIADSDK"

echo "******* clean ZIADSDK ******"

# echo "******* 切换到xcode4.3.2，SDK5.1环境下编译 ******"
# sudo xcode-select -switch /Developer/xcode4.3.2/Xcode4.3.2.app/Contents/Developer/
# xcodebuild clean -target AderSDK -configuration Debug -sdk iphoneos5.1
# xcodebuild clean -target AderSDK -configuration Release -sdk iphoneos5.1

echo "******* create Build Directory ******"
rm -rdf "${buildDir}"
mkdir "${buildDir}"

echo "******* 切换到xcode5.1.1，SDK7.1环境下编译 ******"
xcode_build_sdk="/Applications/Xcode5.1.1.app/Contents/Developer/Platforms"

xcodebuild clean -target ADSDK -configuration Debug -sdk "${xcode_build_sdk}"/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
xcodebuild clean -target ADSDK -configuration Release -sdk "${xcode_build_sdk}"/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
xcodebuild clean -target ADSDK -configuration Debug -sdk "${xcode_build_sdk}"/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk
xcodebuild clean -target ADSDK -configuration Release -sdk "${xcode_build_sdk}"/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk


echo "*************************************"
echo ">>>>>> iPhoneOS building"
xcodebuild -target ADSDK -configuration Release  -sdk "${xcode_build_sdk}"/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
iPhoneLibPath="${buildDir}/${libName}_iPhone.a"
cp ./build/Release-iphoneos/libADSDK.a "${iPhoneLibPath}"
echo "****LibDir=$iPhoneLibPath ******"
lipo -info "${iPhoneLibPath}"

echo "*************************************"
echo ">>>>>> iPhoneSimulator building"
xcodebuild -target ADSDK -configuration Release -sdk "${xcode_build_sdk}"/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk
SimulatorLibPath="${buildDir}/${libName}_Simulator.a"
cp ./build/Release-iphonesimulator/libADSDK.a "${SimulatorLibPath}"
echo "****LibDir=$SimulatorLibPath ******"
lipo -info "${SimulatorLibPath}"


echo "*************************************"
echo "*******create all arch library *******"
allArchLibPath="${buildDir}/${libName}.a"
lipo -create "${iPhoneLibPath}" "${SimulatorLibPath}" -output "${allArchLibPath}"
echo "****LibDir=$allArchLibPath ******"
lipo -info "${allArchLibPath}"
lipo -detailed_info "${allArchLibPath}"

exit
 
echo "*******Building static library complete ******"
rm -rdf "./build"

echo "*******拷贝发布lib，生成发布包 ******"
cp ./PackageDir/libAderSDK.a ../AderSDKPackageRelease/lib/
cp GAdsSDK/AderBanner/AderSDK.h ../AderSDKPackageRelease/lib/
cp GAdsSDK/AderBanner/AderDelegateProtocal.h ../AderSDKPackageRelease/lib/
cp GAdsSDK/AderInterstitial/AderInterstitial.h ../AderSDKPackageRelease/lib/
cp GAdsSDK/AderInterstitial/AderInterstitialDelegate.h ../AderSDKPackageRelease/lib/
cp -r ../AderSDKPackageRelease/lib/ ../AderSDKPackageRelease/AderSample/AderSample/lib/

echo " "
echo "================================================================="
echo "*******最新lib及文件已生成，Sample程序下lib已更新，请验证Sample工程确保可用 ******"
echo "*******开始将CP发布包打包到桌面 ******"
mkdir  ./ader-ios-sdk-${version}
cp -r ../AderSDKPackageRelease  ./ader-ios-sdk-${version}/
rm -rf ./Desktop/ader-ios-sdk-${version}/.DS_Store
rm -rf ../AderSDKPackageRelease/.DS_Store
zip -r ~/Desktop/ader-ios-sdk-${version}.zip ./ader-ios-sdk-${version}
rm -rdf ./ader-ios-sdk-${version}

echo "*******Ader广告平台CP发布包（ader-ios-sdk）打包完成，已存放在桌面******"




